import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet =null;
            //1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 2.获取连接
            String url = "jdbc:mysql://127.0.0.1:3306/lib_dtb?characterEncoding=utf-8";
            // .var
            connection = DriverManager.getConnection(url, "root", "3141");
//            if (connection != null) {
//                System.out.println("连接成功");
//            }
            // 3.写sql
            String sql = "select * from admin";

            // 4.预编译sql
            preparedStatement = connection.prepareStatement(sql);

            // 5.执行sql
            resultSet = preparedStatement.executeQuery();

            // 6.遍历结果集
           while(resultSet.next()){
               int id =resultSet.getInt(1);
               String username = resultSet.getString(2);
               String password = resultSet.getString(3);
               System.out.println(id+"----"+username+"----"+password);
           }




        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }//finally不论程序是否异常都执行
        finally {// 7.关闭资源
            try {
                if(resultSet!=null){
                resultSet.close();}

                if(preparedStatement!=null){
                preparedStatement.close();}

                if(connection!=null){
                connection.close();}

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }


        }
    }
    }

